/*
 * ScrollPane.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

/**
 * Scroll Pane Component.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class ScrollPane extends Component {

  // TODO: add other scroll pane methods

// Private Fields. -------------------------------------------------------------

  private Component view;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new ScrollPane object.
   */
  public ScrollPane() {
    // empty constructor body
  }

  /**
   * Constructs new ScrollPane object.
   *
   * @param view the view component
   */
  public ScrollPane(Component view) {
    if (view != null) {
      acquireComponent(view);
      _setView(_delegate, view._delegate); // NOPMD
      this.view = view;
      view.setSize(view.getPreferredSize());
    }
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns view component.
   *
   * @return view component
   */
  public Component getView() {
    return view;
  }

  /**
   * Sets new view component.
   *
   * @param view the view component
   *
   * @return returns old view component
   */
  public Component setView(Component view) {
    final Component oldView = this.view;
    if (oldView != view) { // NOPMD
      disposedCheck();
      releaseComponent(oldView);
      if (view != null) {
        if (view.isDisposed()) { // NOPMD
          throw new IllegalArgumentException("View component disposed");
        }
        acquireComponent(view);
        _setView(_delegate, view._delegate);
        this.view = view;
        view.setSize(view.getPreferredSize());
      }
    }
    return oldView;
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public void setComponentOrientation(ComponentOrientation orientation) {
    super.setComponentOrientation(orientation);
    if (view != null) {
      view.setComponentOrientation(orientation);
    }
  }

  /**
   * {@inheritDoc}
   */
  public void validate() {
    if (!valid) {
      disposedCheck();
      valid = true;
      if (view != null) {
        view.validate();
      }
    }
  }

  /**
   * {@inheritDoc}
   */
  public void updateLocalization() {
    super.updateLocalization();
    if (view != null) {
      view.updateLocalization();
    }
  }

  /**
   * {@inheritDoc}
   */
  public Component[] getOwnedComponents() {
    if (view != null) {
      return new Component[] {view};
    }
    return EMPTY_COMPONENT_ARRAY; // NOFB NOPMD
  }

  void releaseComponent(Component component) {
    if ((component != null) && (component == view)) { // NOFB NOPMD
      _setView(_delegate, 0);
      view = null;
      component.owner = null;
    }
  }

  /*!
   * @cute4j.native
   *    overridden="true"
   */
  void _styleChanged() {
    super._styleChanged();
    if (view != null) {
      view._styleChanged();
    }
  }

  /*!
   * @cute4j.native
   *    overridden="true"
   */
  void _styleSheetChanged(boolean removed) {
    super._styleSheetChanged(removed);
    if (view != null) {
      view._styleSheetChanged(removed);
    }
  }

// Package-Private Native Methods. ---------------------------------------------

  native long _create();

  native void _setView(long delegate, long viewDelegate);

}
